package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.constant.RegStuBizTypeConstant;
import com.bt.lms.entity.RegStuInfo;
import com.bt.lms.entity.page.Page;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.RegStuInfoService;
import com.bt.lms.service.impl.RegStuInfoServiceImpl;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.SQLException;

/**
 * 报名学员模块实现  控制器
 *
 * @author 朱传博
 */
public class RegStuModuleHandler implements ModuleHandler {


    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {

        //1.获取业务类型
        Short bizType = reqMsg.getBizType();
        //2.处理数据
        String data = reqMsg.getData();

        //3.调用业务接口
        Object obj = callBizType(data, bizType);

        //获取响应
        Byte twoWay = reqMsg.getTwoWay();

        //如果需要响应，把响应消息返回去
        if (twoWay == ReqMsg.NETWORK_TWO_WAY_CHAT) {


            RespMsg respMsg = objToRespMsg(reqMsg, obj);
            //响应消息转成byte数组
            byte[] bytes = respMsg.toBytes();

            try {
                OutputStream out = socket.getOutputStream();
                out.write(bytes);
                out.flush();

                //关流
                out.close();
                socket.close();

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }


    /**
     * 对象转成响应消息
     *
     * @param reqMsg 消息
     * @param obj    对象
     * @return 响应消息
     */
    private RespMsg objToRespMsg(ReqMsg reqMsg, Object obj) {

        //响应消息
        RespMsg respMsg = new RespMsg();

        //魔数
        String magicNo = reqMsg.getMagicNo();
        respMsg.setMagicNo(magicNo);

        //协议版本号
        Byte version = reqMsg.getVersion();
        respMsg.setVersion(version);

        //序列化算法
        Byte serialCou = reqMsg.getSerialCou();
        respMsg.setSerialCou(serialCou);

        //事件类型
        Byte evenType = reqMsg.getEvenType();
        respMsg.setEvenType(evenType);

        //业务类型
        Short bizType = reqMsg.getBizType();
        respMsg.setBizType(bizType);

        //状态
        respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);

        //请求id
        Long reqId = reqMsg.getReqId();
        respMsg.setReqId(reqId);

        //保留字段
        byte[] remainField = reqMsg.getRemainField();
        respMsg.setRemainField(remainField);

        //数据
        String data = JSON.toJSONString(obj);
        respMsg.setData(data);


        return respMsg;
    }

    /**
     * 调用业务接口
     *
     * @param data    数据
     * @param bizType 业务类型
     */
    private Object callBizType(String data, short bizType) {


        RegStuInfoService regStuInfoService = new RegStuInfoServiceImpl();


        Object obj = null;

        if (bizType == RegStuBizTypeConstant.STU_REG_INFO_PAGING_QUERY) {

            Page page = JSON.parseObject(data, Page.class);

            try {
                obj = regStuInfoService.pagingConditionQuery(page);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (bizType == (RegStuBizTypeConstant.STU_REG_INFO_ADD)) {
            RegStuInfo regStuInfo = JSON.parseObject(data, RegStuInfo.class);



            //调用添加方法
            try {
                obj = regStuInfoService.add(regStuInfo);

            } catch (Exception e) {
//                throw new RuntimeException(e);
            }

        } else if (bizType == RegStuBizTypeConstant.STU_REG_INFO_EDIT) {

            RegStuInfo editRegStuInfo = JSON.parseObject(data, RegStuInfo.class);
            //要编辑的报名学员id
            Long editRegStuId = editRegStuInfo.getId();
            try {
                obj = regStuInfoService.edit(editRegStuId, editRegStuInfo);

            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        if (bizType == RegStuBizTypeConstant.STU_REG_INFO_DELETE) {

        }
        if (bizType == RegStuBizTypeConstant.STU_REG_INFO_ENROLL) {

        }
        if (bizType == RegStuBizTypeConstant.STU_REG_INFO_QUERY_MATERIAL) {

        }
        if (bizType == RegStuBizTypeConstant.STU_REG_INFO_QUERY_DETAIL) {


            RegStuInfo queryDetailRegStuID = JSON.parseObject(data, RegStuInfo.class);
            //获取到要查看报名学员的id
            Long regStuInfoId = queryDetailRegStuID.getId();
            try {
                //调用查看详情方法
                obj = regStuInfoService.queryDetail(regStuInfoId);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }

        }

        return obj;
    }


}
